<template>
    <div>
        <el-descriptions title="基本信息" :column="3" style="margin-top: 10px;">
            <el-descriptions-item label="用户名:" :span="3">
                {{ info.userinfo.username }}
                <img v-if="info.userinfo.auth_type" src="../../../assets/enterprise/yirenzheng.png"
                    style="margin-left: 5px;" />
                <img v-else src="../../../assets/enterprise/weirenzheng.png" style="margin-left: 5px;" />
                <el-button size="small" class="btn" style="margin-left: 20px;"
                    v-if="info.role.role_id != 1 && companyDataStore.companyList.length > 0"
                    @click="addpersonvisible = true">完善资料</el-button>
            </el-descriptions-item>
            <el-descriptions-item label="登录账号:">{{ info.userinfo.mobile }}</el-descriptions-item>
            <el-descriptions-item label="账号角色:" v-if="info.role">{{ info.role.role_name }}</el-descriptions-item>
        </el-descriptions>
        <el-descriptions title="企业信息" :column="3"
            style="padding-top: 20px; border-top: solid 1px #ebeef5; margin-top: 10px;" v-if="info.info">
            <el-descriptions-item label="企业名称:">{{ info.info.name }}</el-descriptions-item>
            <el-descriptions-item label="统一社会信用代码:">{{ info.info.social_credit_code }}</el-descriptions-item>
            <el-descriptions-item label="法定代表人姓名:">{{ info.info.legal_person }}</el-descriptions-item>
            <el-descriptions-item label="法定代表人身份证号:">{{ info.info.legal_person_id }}</el-descriptions-item>
            <el-descriptions-item label="法定代表人手机号:">{{ info.info.mobile }}</el-descriptions-item>
        </el-descriptions>
        <el-descriptions title="认证信息" :column="3"
            style="padding-top: 20px; border-top: solid 1px #ebeef5; margin-top: 10px;"
            v-if="info.userinfo.auth_type == 1">
            <el-descriptions-item label="企业名称:">{{ info.userinfo.auth_name }}</el-descriptions-item>
            <el-descriptions-item label="统一社会信用代码:">{{ info.userinfo.social_credit_code }}</el-descriptions-item>
            <el-descriptions-item label="企业注册时间:">{{ info.userinfo.establish_time }}</el-descriptions-item>
            <el-descriptions-item label="法定代表人姓名:">{{ info.userinfo.legal_person }}</el-descriptions-item>
            <!-- <el-descriptions-item label="法定代表人身份证:">{{ info.userinfo.legal_person_id }}</el-descriptions-item> -->
            <el-descriptions-item label="法定代表人手机号:">{{ info.userinfo.mobile }}</el-descriptions-item>
            <el-descriptions-item label="认证时间:">{{ info.userinfo.create_time }}</el-descriptions-item>
        </el-descriptions>
        <el-descriptions title="认证信息" :column="3"
            style="padding-top: 20px; border-top: solid 1px #ebeef5; margin-top: 10px;" v-else>
            <el-descriptions-item label="个人姓名:">{{ info.userinfo.auth_name }}</el-descriptions-item>
            <el-descriptions-item label="个人手机号:">{{ info.userinfo.mobile }}</el-descriptions-item>
            <!-- <el-descriptions-item label="个人身份证:">{{ info.userinfo.id_card }}</el-descriptions-item> -->
        </el-descriptions>
        <el-descriptions title="安全设置" :column="1"
            style="padding-top: 20px; border-top: solid 1px #ebeef5; margin-top: 10px;">
            <el-descriptions-item>
                <div class="security">
                    <el-icon :size="16" color="#2962a5">
                        <Paperclip />
                    </el-icon>
                    <div class="tit">签署密码</div>
                    <div class="status">{{ info.userinfo.sign_password ? '已设置' : '未设置'}}</div>
                    <span>{{ info.userinfo.sign_password ? '******' : ''}}</span>
                    <el-button size="small" class="btn" @click="signPasswordDialog = true">{{ info.userinfo.sign_password ? '修改签署密码' : '设置签署密码'}}</el-button>
                </div>
            </el-descriptions-item>
            <el-descriptions-item>
                <div class="security">
                    <el-icon :size="16" color="#2962a5">
                        <Iphone />
                    </el-icon>
                    <div class="tit">绑定手机号</div>
                    <div class="status">已设置</div>
                    <span>{{ info.userinfo.mobile }}</span>
                    <el-button size="small" class="btn" @click="mobileDialog = true">换绑手机号</el-button>
                </div>
            </el-descriptions-item>
            <el-descriptions-item>
                <div class="security">
                    <el-icon :size="16" color="#2962a5">
                        <Lock />
                    </el-icon>
                    <div class="tit">账号密码</div>
                    <div class="status">已设置</div>
                    <span>******</span>
                    <el-button size="small" class="btn" @click="pwdDialog = true">修改密码</el-button>
                </div>
            </el-descriptions-item>
            <!-- <el-descriptions-item>
                <div class="security">
                    <el-icon :size="16" color="#2962a5">
                        <Warning />
                    </el-icon>
                    <p>转移实名信息其他手机号</p>
                </div>
            </el-descriptions-item> -->
        </el-descriptions>
        <el-dialog v-model="mobileDialog" :close-on-click-modal="false" width="500px" title="更换绑定手机号" :z-index="9">
            <el-form :model="form" ref="ruleFormRef" :inline="true" label-width="80px">
                <el-form-item label="手机号" prop="mobile" maxlength="11" style="width: 100%;">
                    <el-input v-model="form.mobile" placeholder="请输入手机号" style="width: 100%;"></el-input>
                </el-form-item>
                <el-form-item label="验证码" prop="code" style="width: 100%">
                    <el-input v-model="form.code" placeholder="请输入验证码" style="flex: 1; margin-right: 5px;" />
                    <CountDownBtn :phone="form.mobile" :type="3" />
                </el-form-item>
                <el-form-item label="登录密码" prop="password" style="width: 100%;">
                    <el-input type="password" v-model="form.password" placeholder="请输入登录密码"
                        style="width: 100%;"></el-input>
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-foot">
                    <el-button @click="mobileDialog = false">取消</el-button>
                    <el-button type="primary" :loading="loading" @click="handleVerify">
                        确定
                    </el-button>
                </div>
            </template>
        </el-dialog>
        <el-dialog v-model="pwdDialog" :close-on-click-modal="false" width="500px" title="修改登录密码" :z-index="9">
            <el-form :model="pwdForm" ref="pwdFormRef" :inline="true" label-width="95px">
                <el-form-item label="原密码" prop="oldPwd" style="width: 100%;">
                    <el-input v-model="pwdForm.oldPwd" placeholder="请输入原密码" style="width: 100%;" type="password" />
                </el-form-item>
                <el-form-item label="新密码" prop="newPwd" style="width: 100%;">
                    <el-input v-model="pwdForm.newPwd" placeholder="请输入新密码" style="width: 100%;" type="password" />
                </el-form-item>
                <el-form-item label="确认新密码" prop="newPwd2" style="width: 100%;">
                    <el-input v-model="pwdForm.newPwd2" placeholder="请再次输入新密码" style="width: 100%;" type="password" />
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-foot">
                    <el-button @click="pwdDialog = false">取消</el-button>
                    <el-button type="primary" :loading="loading" @click="handlePwd">
                        确定
                    </el-button>
                </div>
            </template>
        </el-dialog>
        <Addperson v-model:visible="addpersonvisible" :isedit="true" :iswanshan="true" :infoid="info.userinfo.info_id"
            v-if="info.role.role_id != 1"></Addperson>
        <el-dialog v-model="signPasswordDialog" :close-on-click-modal="false" width="600px" :title="info.userinfo.sign_password ? '修改签署密码' : '设置签署密码'" :z-index="9">
          <div class="sign-password-dialog">
            <div class="step-container">
              <div class="step" :class="{cur: signPasswordStep == 1}">
                <div class="step-num">1</div>
                <div class="step-title">验证手机号</div>
              </div>
              <div class="line"></div>
              <div class="step" :class="{cur: signPasswordStep == 2}">
                <div class="step-num">2</div>
                <div class="step-title">设置签署密码</div>
              </div>
              <div class="line"></div>
              <div class="step" :class="{cur: signPasswordStep == 3}">
                <div class="step-num">3</div>
                <div class="step-title">设置成功</div>
              </div>
            </div>
            <div v-if="signPasswordStep == 1">
              <div class="tip">设置签署密码之前，需要验证当前绑定的手机号码</div>
              <el-form :model="signPwdVerifyForm" :inline="true" label-width="65px">
                  <el-form-item label="手机号" prop="mobile" maxlength="11" style="width: 100%;">
                      <el-input v-model="signPwdVerifyForm.mobile" placeholder="请输入手机号" style="width: 100%;"></el-input>
                  </el-form-item>
                  <el-form-item label="验证码" prop="code" style="width: 100%">
                      <el-input v-model="signPwdVerifyForm.code" placeholder="请输入验证码" style="flex: 1; margin-right: 5px;" />
                      <CountDownBtn :phone="signPwdVerifyForm.mobile" :type="6" />
                  </el-form-item>
              </el-form>
            </div>
            <div v-if="signPasswordStep == 2" style="margin-top: 20px;">
              <el-form-item label="签署密码" style="width: 100%;">
                    <el-input v-model="signPwdForm.sign_password" placeholder="请输入签署密码" style="width: 100%;" type="password" />
                </el-form-item>
                <el-form-item label="确认密码" style="width: 100%;">
                    <el-input v-model="signPwdForm.sign_password_confirm" placeholder="请再次输入签署密码" style="width: 100%;" type="password" />
                </el-form-item>
            </div>
            <div v-if="signPasswordStep == 3" style="margin: 20px 0;">
              <div class="success">
                <el-icon size="80" color="#2962a5"><SuccessFilled /></el-icon>
              </div>
            </div>
            <div class="dialog-foot">
                  <el-button type="primary" :loading="loading" @click="handleSignPwd">
                      {{ signPasswordStep == 1 ? '下一步' : signPasswordStep == 2 ? '设置签署密码' : '完成'}}
                  </el-button>
              </div>
          </div>
        </el-dialog>
    </div>
</template>
<script lang="ts" setup>
import { ChangePhone, ChangePwd, SetSignPwd, UserBaseInfo, VerifyCode } from '@/api/user';
import CountDownBtn from '@/components/CountDownBtn.vue';
import { useCompanyDataStore } from '@/store/companys';
import { ElMessage } from 'element-plus';
import { onMounted, reactive, ref } from 'vue';
import Addperson from "../../Employees/components/Addperson.vue";

const companyDataStore = useCompanyDataStore()

const info = ref({
    info: {
        id: 0,
        mobile: '',
        name: '',
        legal_person: '',
        legal_person_id: '',
        social_credit_code: ''
    },
    role: {
        role_id: 0,
        role_name: ''
    },
    userinfo: {
        username: '',
        auth_type: 1,
        auth_name: '',
        create_time: '',
        id_card: '',
        mobile: '',
        legal_person: '',
        establish_time: '',
        legal_person_id: '',
        social_credit_code: '',
        id: 0,
        uid: 0,
        info_id: 0,
        sign_password: ''
    }
})
const mobileDialog = ref(false)
const form = reactive({
    mobile: '',
    code: '',
    password: ''
})
const loading = ref(false)
const pwdDialog = ref(false)
const pwdForm = reactive({
    oldPwd: '',
    newPwd: '',
    newPwd2: ''
})
const addpersonvisible = ref(false);
const signPasswordDialog = ref(false)
const signPwdVerifyForm = reactive({
  mobile: '',
  code: ''
})
const signPasswordStep = ref(1)
const signPwdForm = reactive({
  sign_password: '',
  sign_password_confirm: ''
})

const handleVerify = () => {
    if (form.mobile == '') {
        ElMessage.error('请输入手机号')
        return
    }
    if (!/^1[3456789]\d{9}$/.test(form.mobile)) {
        ElMessage.error('手机号格式不正确')
        return
    }
    if(form.code == '') {
        ElMessage.error('请输入验证码')
        return
    }
    if(form.password == '') {
        ElMessage.error('请输入登录密码')
        return
    }
    loading.value = true
    ChangePhone(form).then((res) => {
        loading.value = false
        ElMessage.success('更换成功')
        mobileDialog.value = false
        getInfo()
    }).catch((err) => {
        loading.value = false
    })
}

const getInfo = async () => {
    await UserBaseInfo({}).then((res) => {
        info.value = res.data
    })
}

const handlePwd = () => {
    if(pwdForm.oldPwd == '') {
        ElMessage.error('请输入原密码')
        return
    }
    if(pwdForm.newPwd == '') {
        ElMessage.error('请输入新密码')
        return
    }
    if(pwdForm.newPwd2 == '') {
        ElMessage.error('请再次输入新密码')
        return
    }
    if(pwdForm.newPwd != pwdForm.newPwd2) {
        ElMessage.error('两次密码不一致')
        return
    }
    loading.value = true
    ChangePwd({
        old_password: pwdForm.oldPwd,
        password: pwdForm.newPwd
    }).then((res) => {
        loading.value = false
        ElMessage.success('修改成功')
        pwdDialog.value = false
    }).catch((err) => {
        loading.value = false
    })
}

const handleSignPwd = () => {
  if(signPasswordStep.value == 1) {
    if(signPwdVerifyForm.mobile == '') {
        ElMessage.error('请输入手机号')
        return
    }
    if (!/^1[3456789]\d{9}$/.test(signPwdVerifyForm.mobile)) {
        ElMessage.error('手机号格式不正确')
        return
    }
    if(signPwdVerifyForm.code == '') {
        ElMessage.error('请输入验证码')
        return
    }
    loading.value = true
    VerifyCode({
      mobile: signPwdVerifyForm.mobile,
      code: signPwdVerifyForm.code
    }).then(res => {
      loading.value = false
      signPasswordStep.value = 2
    }).catch(() => {
      loading.value = false
    })
  }
  if(signPasswordStep.value == 2) {
    if(signPwdForm.sign_password == '') {
        ElMessage.error('请输入签署密码')
        return
    }
    if(signPwdForm.sign_password_confirm == '') {
        ElMessage.error('请输入确认签署密码')
        return
    }
    if(signPwdForm.sign_password != signPwdForm.sign_password_confirm) {
        ElMessage.error('两次密码不一致')
        return
    }
    loading.value = true
    SetSignPwd({
      sign_password: signPwdForm.sign_password
    }).then(res => {
      loading.value = false
      signPasswordStep.value = 3
      ElMessage.success('设置成功')
      getInfo()
    }).catch(() => {
      loading.value = false
    })
  }  
  if(signPasswordStep.value == 3) {
    signPasswordStep.value = 1
    signPasswordDialog.value = false
  }
}

onMounted(() => {
    getInfo()
})
</script>
<style lang="scss" scoped>
.security{
    padding-left: 35px;
    padding-bottom: 10px;
    display: flex;
    align-items: center;

    .tit{
        margin-left: 2px;
        width: 120px;
    }

    .status{
        width: 100px;
    }

    span{
        width: 160px;
    }

    p{
        font-size: 12px;
        color: #2962a5;
        margin-left: 2px;
        cursor: pointer;
    }

    .btn{
        width: 100px;
    }
}

.sign-password-dialog{
  padding: 0 40px;

  .step-container{
    display: flex;
    justify-content: center;
    align-items: center;
    padding-top: 10px;

    .step{
      display: flex;
      align-items: center;

      &.cur{
        .step-num{
          background-color: #2962a5;
          color: #fff;
        }
        .step-title{
          color: #333;
        }
      }

      .step-num{
        font-size: 16px;
        color: #666;
        width: 36px;
        height: 36px;
        border-radius: 50%;
        background-color: #eee;
        text-align: center;
        line-height: 36px;
        margin-right: 10px;
      }

      .step-title{
          color: #999;
          font-size: 16px;
        }
    }

    .line{
      width: 30px;
      height: 1px;
      background-color: #eee;
      margin: 0 10px;
    }
  }

  .tip{
      width: 100%;
      text-align: center;
      font-size: 16px;
      color: #666;
      margin: 20px 0;
  }

  .dialog-foot{
    display: flex;
    justify-content: center;
  }

  .success{
    display: flex;
    justify-content: center;
  }
}


</style>